#include "Protocol.hpp"
#include "TcpServer.hpp"
#include "NetCal.hpp"
#include "Daemon.hpp"
#include <memory>

void Usage(std::string proc)
{
    std::cerr << "Usage: " << proc << " port" << std::endl;
}

//./tcpserver
int main(int argc, char*argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }

    std::cout << "已经是一个守护进程了"<< std::endl;
    //Daemon();
    daemon(0,0);//系统调用

    //Enable_Console_Log_Strategy();

    Enable_File_Log_Strategy();

    std::unique_ptr<Cal> cal = std::make_unique<Cal>();

    std::unique_ptr<Protocol> protocolptr = std::make_unique<Protocol>([&cal](Request &req)->Response{
        return cal->Execute(req);
    });

    std::unique_ptr<TcpServer> tsvr = std::make_unique<TcpServer>(std::stoi(argv[1]), 
    [&protocolptr](std::shared_ptr<Socket> &sock, InetAddr &client){
        protocolptr->GetRequest(sock, client);
    });

    tsvr->Start();
    return 0;
}